import codecs
import re
from tqdm import *

dic = {}

# reg = re.compile('^.{9}(.*)$')
with codecs.open('../hdf5/2017-09-25.csv', 'r', 'utf-8') as f:
    lines = f.readlines()
    for line in tqdm(lines):
        a = line.split(',')
        code = a[0]
        item = dic.get(code)
        if item is None:
            dic[code] = 'datetime,open,closeYesterday,current,high,low,ask,bid,volume,turnover,ask1Volume,ask1Price,ask2Volume,ask2Price,ask3Volume,ask3Price,ask4Volume,ask4Price,ask5Volume,ask5Price,bid1Volume,bid1Price,bid2Volume,bid2Price,bid3Volume,bid3Price,bid4Volume,bid4Price,bid5Volume,bid5Price,type'
        else:
            dic[code] = item + a[31] + ' ' + a[32] + ',' + ','.join(a[2:31]) + ',' + a[33]

#         a = line[0:-1].split(',')
#         code = a[0]
#         file = codecs.open('./stocks/'+code+'.csv', 'a')
#         file.writelines()
#         file.write(','.join(a[2:34])+'\n')
#         file.close()
#
# for line in codecs.open('../hdf5/2017-09-25.csv', 'r', 'utf-8'):
#     a = line.split(',')
#     code = a[0]
#     dic[code] = ('' if dic.get(code) is None else dic.get(code)) + ','.join(a[2:34])
#
for code, val in dic.items():
    file = codecs.open('./stocks/'+code+'.csv', 'a', 'utf-8')
    file.write(val)
    file.close()
